﻿package com.potocrewz.loaders
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import gs.easing.Sine;
	import gs.TweenMax;
	
	/**
	 * ...
	 * @author Adrien Lamotte
	 */
	public class LoadMe extends MovieClip
	{
		
		private var _conteneurImage:Loader;// création du conteneur de l'image
		private var _clipConteneur:MovieClip;
		private var image:URLRequest;
		
		private var my_sprite:MovieClip;
		private var my_x:int;
		private var my_y:int;
		private var txtPourcent:TextField;
		
		
		private var my_tween:TweenMax;		
		private var hide_begining:Boolean;
		private var _url:String;
		
		
		
		public function LoadMe(pUrl:String, pFlashVars:String, pConteneur:MovieClip, pX:int = 0, pY:int = 0, hide_at_begining:Boolean = false ) 
		{
			_url = pUrl;
			hide_begining = hide_at_begining;
			
			image = new URLRequest(pUrl);
			if(pFlashVars != ""){
				image.method = "GET";
				image.data = new URLVariables(pFlashVars);  
			}  
			
			clipConteneur = pConteneur;
			
			my_x = pX;
			my_y = pY;
			
			my_sprite = new MovieClip();
			my_sprite.x = my_x;
			my_sprite.y = my_y;
			clipConteneur.addChild(my_sprite);
			
			
			// Création du texte pour l'affichage du pourcentage
				txtPourcent = new TextField();
				txtPourcent.autoSize = TextFieldAutoSize.LEFT;
				txtPourcent.textColor = 0xFFFFFF;
				txtPourcent.alpha = 0.5;
				txtPourcent.x = 5;
				txtPourcent.y = 6;
				my_sprite.alpha = 0;
				
				my_sprite.addChild(txtPourcent);
			//___________________________________________________
			
			
			conteneurImage = new Loader();
			
			conteneurImage.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);

			conteneurImage.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);

			conteneurImage.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, indiquerErreur);

			
			// chargement de l'image dans le conteneur
			conteneurImage.load(image);
			
		}
		
		function indiquerErreur ( erreur:Event ):void
		 {
			trace( "Erreur lors du chargement : "+ erreur );
		}
		
		// Le chargement est terminé
		function onCompleteHandler(evt:Event):void
		{
			//Suppression des infos de progression
			my_sprite.removeChild(txtPourcent);
			// affichage du conteneur
			
			
			if (_url.indexOf(".swf") != -1 ) {
				// affichage du conteneur
				my_sprite.addChild(conteneurImage);
			}
			else {
				var oldImageData: BitmapData = evt.target.content.bitmapData
				var bitmapHolder:Bitmap = new Bitmap( oldImageData , "auto", true);
				my_sprite.addChild(bitmapHolder);
			}
			
			
			if(!hide_begining){
				TweenMax.to(my_sprite, 1.1, { autoAlpha:1, ease:Sine.easeOut } );
			}
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		
		// Progression du chargement
		function onProgressHandler(evt:ProgressEvent):void
		{
			
			txtPourcent.text = Math.round((evt.bytesLoaded / evt.bytesTotal)*100) + "%";//affichage du pourcentage
		}
		
		public function get sprite():MovieClip { return my_sprite; }
		
		public function set sprite(value:MovieClip):void 
		{
			my_sprite = value;
		}
		
		public function get clipConteneur():MovieClip { return _clipConteneur; }
		
		public function set clipConteneur(value:MovieClip):void 
		{
			_clipConteneur = value;
		}
		
		public function get conteneurImage():Loader { return _conteneurImage; }
		
		public function set conteneurImage(value:Loader):void 
		{
			_conteneurImage = value;
		}
		
	}
	
}